package com.gourd.common.rbac.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gourd.common.exception.EntityExistException;
import com.gourd.common.rbac.dao.RbacUserDao;
import com.gourd.common.rbac.dao.RbacUserRoleDao;
import com.gourd.common.rbac.dto.RbacUserDTO;
import com.gourd.common.rbac.dto.RbacUserSearchDTO;
import com.gourd.common.rbac.entity.RbacUser;
import com.gourd.common.rbac.entity.RbacUserRole;
import com.gourd.common.rbac.service.RbacUserService;
import com.gourd.common.rbac.vo.JwtUser;
import com.gourd.common.utils.IdGeneratorUtil;
import com.gourd.common.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 用户处理业务层
 *
 * @author gourd
 * @date 2019-04-02 17:26:16
 * Created by CodeGen .
 */
@Service
@Slf4j
public class RbacUserServiceImpl extends ServiceImpl<RbacUserDao, RbacUser> implements RbacUserService {

    @Autowired
    private RbacUserDao rbacUserDao;

    @Autowired
    private RbacUserRoleDao rbacUserRoleDao;

    @Autowired
    private IdGeneratorUtil idGeneratorUtil;

    @Override
    public RbacUser getByAccount(String account){
        return  rbacUserDao.getByAccount(account);
    }


    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @Override
    public RbacUser getById(Long id){
        return  rbacUserDao.selectById(id);
    }
    /**
     * 获取当前用户信息
     * @return
     */
    @Override
    public JwtUser getCurrent(){
        return JwtUtil.getUserInfo();
    }
    /**
     * 获取所有用户信息
     * @return
     */
    @Override
    public List<RbacUser> findAll(){
        return  rbacUserDao.findAll();
    }
    /**
     * 根据条件获取用户
     * @return
     */
    @Override
    public Page<RbacUser> find(RbacUserSearchDTO rbacUserDTO, PageRequest pageRequest ){
        Page<RbacUser> page = new Page<>();
        //每页数
        page.setSize(pageRequest.getPageSize());
        //当前页码
        page.setCurrent(pageRequest.getPageNumber());
        page.setSearchCount(true);
        EntityWrapper<RbacUser> qryWapper = new EntityWrapper<>();
        if(StringUtils.isNotEmpty(rbacUserDTO.getAccount())){
            qryWapper.eq("account", rbacUserDTO.getAccount());
        }
        if(StringUtils.isNotEmpty(rbacUserDTO.getName())){
            qryWapper.like("name", rbacUserDTO.getAccount());
        }
        List<RbacUser> rbacUsers = rbacUserDao.selectPage(page,qryWapper);
        page.setRecords(rbacUsers);
        return page;
    }
    /**
     * 保存用户
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RbacUser register(RbacUserDTO rbacUserDTO){
        RbacUser rbacUser = new RbacUser();
        BeanUtils.copyProperties(rbacUserDTO,rbacUser);
        // 设置一个id
        rbacUser.setId(idGeneratorUtil.getSeqId());
        if(rbacUserDao.getByAccount(rbacUser.getAccount())!=null) {
            throw new EntityExistException(RbacUser.class,"account",rbacUser.getAccount());
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        final String rawPassword = rbacUserDTO.getPassword();
        rbacUser.setPassword(encoder.encode(rawPassword));
        rbacUser.setLastPasswordResetDate(new Date());
        // 保存主表用户信息
        rbacUserDao.insert(rbacUser);
        // 设置默认的 角色
        long roleId = 3;
        RbacUserRole rbacUserRole = new RbacUserRole();
        rbacUserRole.setId(idGeneratorUtil.getSeqId());
        rbacUserRole.setUserId(rbacUser.getId());
        rbacUserRole.setRoleId(roleId);
        rbacUserRoleDao.insert(rbacUserRole);
        return rbacUser;
    }

    /**
     * 删除用户
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long  id){
        Long userId = id;
        // 删除用户
        rbacUserDao.deleteById(id);
        // 删除用户角色
        rbacUserRoleDao.deleteById(userId);
    }
}